package com.rentalcar.rental_car.utils;

import java.util.Date;
import java.util.Map;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;

public class TokenUtils {
    private static String signature = "myTokenKey";
    private static long time = 1000 * 60 * 60 * 24;


    public static String getToken(Map<String, String> map) {
        JWTCreator.Builder builder = JWT.create();

        //payload
        map.forEach((k, v) -> {
            builder.withClaim(k, v);
        });
        //设置token过期时间为1天
        builder.withExpiresAt(new Date(System.currentTimeMillis() + time));
        return builder.sign(Algorithm.HMAC256(signature));
    }


    public static void check(String token) {
        JWT.require(Algorithm.HMAC256(signature)).build().verify(token);
    }


    public static DecodedJWT getToken(String token) {
        return JWT.require(Algorithm.HMAC256(signature)).build().verify(token);
    }
}
